package com.kstech.mp3604.base

import com.blankj.utilcode.util.SPUtils

object Global {
    /**
     * 服务器ip 端口
     */
//    val ipSetting = "192.168.1.4"
    val ipSetting = "192.168.1.3"
    val portSetting = 9006

    /**
     * 自动横移位置
     */
    var app_autoHY_value = 0
    const val APP_AUTOHY_VALUE = "autoHY"

    /**
     * 吊装重量
     * 吊装要100kg 的话，预设值要大于100kg
     *
     */
    const val DZ_WEIGHT = 50

    /**
     * 15所测试偏差值x
     * 800 毫米
     */
    const val PIANCHA = 800
    const val PIANCHA_Y = 970

    /**
     * 无线网关 192.168.31.88
     * 有线网关 192.168.100.140
     */
    const val VIDEO_URL_1 = "rtsp://admin:123456@192.168.1.88/cam2/mainstream"
//    const val VIDEO_URL_1 = "rtsp://192.168.31.88:8554/cam2/mainstream"

    /**
     * 内置四种弹药速度限制
     * 横移、起升、接触、回转
     *
     * 0.001m/min 串口 现在已经转化完成
     * m/min socket
     *
     * 最保险速度
     * 如果匹配到弹种，更新15所的速度限制
     */
    val typeNameArr = arrayOf("280", "500", "16A", "YH", "122", "其他")
    val speedQS = arrayOf(10000, 10000, 8000, 10000, 10000, 8000)
    val speedHY = arrayOf(6000, 6000, 6000, 6000, 6000, 6000)
    val speedJC = arrayOf(900, 900, 900, 900, 900, 900)
    val speedHZ = arrayOf(0, 0, 0, 0, 0, 0)

    /**
     * 当前有任务、或者无任务的弹种速度限速规则
     */
    var currentSpeedQS = speedQS[typeNameArr.lastIndex]
    var currentSpeedHY = speedHY[typeNameArr.lastIndex]
    var currentSpeedJC = speedJC[typeNameArr.lastIndex]
//    var currentSpeedHZ = speedHZ[typeNameArr.lastIndex]

    /**
     * 控制器发来的实时速度
     */
    var mSpeedQS = 0
    var mSpeedHY = 0
    var mSpeedJC = 0


    /**
     * 根据弹药需要获取
     * typeNameArr 以该数组排序为准
     */
    fun getSpeedHY(index: Int): Int {
        return SPUtils.getInstance().getInt("SPEED-${typeNameArr[index]}-SPEED_HY")
            .takeIf { it != -1 } ?: speedHY[index]
    }

    fun getSpeedQS(index: Int): Int {
//        val restult = SPUtils.getInstance().getInt("SPEED-${typeNameArr[index]}-SPEED_QS")
//        restult.toString().loge("blankii-88")
        return SPUtils.getInstance().getInt("SPEED-${typeNameArr[index]}-SPEED_QS")
            .takeIf { it != -1 } ?: speedQS[index]
    }

    fun getSpeedJC(index: Int): Int {
        return SPUtils.getInstance().getInt("SPEED-${typeNameArr[index]}-SPEED_JC")
            .takeIf { it != -1 } ?: speedJC[index]
    }

    fun getSpeedHZ(index: Int): Int {
        return SPUtils.getInstance().getInt("SPEED-${typeNameArr[index]}-SPEED_HZ")
            .takeIf { it != -1 } ?: speedHZ[index]
    }

    fun setSpeedHY(index: Int, value: Int) {
        val key = "SPEED-${typeNameArr[index]}-SPEED_HY"
        if (SPUtils.getInstance().getInt(key) == -1) {
            SPUtils.getInstance().put(key, value)
        }
    }

    fun setSpeedQS(index: Int, value: Int) {
        val key = "SPEED-${typeNameArr[index]}-SPEED_QS"
        if (SPUtils.getInstance().getInt(key) == -1) {
            SPUtils.getInstance().put(key, value)
        }
    }

    fun setSpeedJC(index: Int, value: Int) {
        val key = "SPEED-${typeNameArr[index]}-SPEED_JC"
        if (SPUtils.getInstance().getInt(key) == -1) {
            SPUtils.getInstance().put(key, value)
        }
    }

    fun setSpeedHZ(index: Int, value: Int) {
        val key = "SPEED-${typeNameArr[index]}-SPEED_HZ"
        if (SPUtils.getInstance().getInt(key) == -1) {
            SPUtils.getInstance().put(key, value)
        }
    }

    /**
     * 任务单保存条数
     */
    const val OVERTIME_MAX_COUNT = 1000// 保存的最大数据条数，超过的话触发清理
    const val OVERTIME_RETAIN_COUNT = 500// 触发清理的话保留的数据条数

    /**
     * 故障保存条数
     */
    const val ERROR_OVERTIME_MAX_COUNT = 1000// 保存的最大数据条数，超过的话触发清理
    const val ERROR_OVERTIME_RETAIN_COUNT = 500// 触发清理的话保留的数据条数

    /**
     * 操作日志保存条数
     */
    const val LOG_OVERTIME_MAX_COUNT = 1000// 保存的最大数据条数，超过的话触发清理
    const val LOG_OVERTIME_RETAIN_COUNT = 500// 触发清理的话保留的数据条数

    /**
     * 使用SharedPreferences  存储仓库id  String
     */
    const val WARE_HOUSE_ID = "WH_ID"
    var app_wareHouse = ""

    /**
     * 使用SharedPreferences  存储洞库入口位置 0未配置 1左洞库 2右洞库  int
     * 存储spinner 的position
     * 发送通讯前需要提前配置好，否则发出的是0
     */
    const val WARE_HOUSE_SETTING_ID = "WH_SETTING_ID"
    var app_wareHouse_setting = 2

    /**
     * 使用SharedPreferences  IP 端口
     */
    const val SETTING_IP = "SETTING_IP"
    const val SETTING_PORT = "SETTING_PORT"


    /**
     * 解析串口数据
     * 帧头 帧尾 十六进制定义
     */
    const val FRAME_HEADER = "40"
    const val FRAME_END = "1A"

    /**
     * 进入设置-参数设置密码
     */
    const val LOGIN_SET_PARAM_PASSWORD = "123"

    /**
     * socket 设置config sp 存储标识
     */
    const val ONE_LINE_LENGTH = "oneLineLength" //以int类型保存
    const val TWO_LINE_LENGTH = "twoLineLength" //以int类型保存
    const val INNER_ARC = "innerArc"   //以int类型保存
    const val OUTER_ARC = "outerArc"  //以int类型保存
    const val ANGLE = "angle"  //以float类型保存


    //如果使用测试数据，则执行该函数
    fun initConfigParam() {
        if (SPUtils.getInstance().getInt(ONE_LINE_LENGTH) == -1) {
            SPUtils.getInstance().put(ONE_LINE_LENGTH, 0)
        }

        if (SPUtils.getInstance().getInt(TWO_LINE_LENGTH) == -1) {
            SPUtils.getInstance().put(TWO_LINE_LENGTH, 0)
        }
        if (SPUtils.getInstance().getInt(TWO_LINE_LENGTH) == -1) {
            SPUtils.getInstance().put(TWO_LINE_LENGTH, 0)
        }
        if (SPUtils.getInstance().getInt(INNER_ARC) == -1) {
            SPUtils.getInstance().put(INNER_ARC, 0)
        }
        if (SPUtils.getInstance().getInt(OUTER_ARC) == -1) {
            SPUtils.getInstance().put(OUTER_ARC, 0)
        }
        if (SPUtils.getInstance().getFloat(ANGLE) == -1f) {
            SPUtils.getInstance().put(ANGLE, 0f)
        }
    }

    /**
     * 1：待机状态
     * 2：空载状态:货物重量为零
     * 3：带载状态:货物重量有
     * 4：故障状态:控制器传来故障异常
     * 5 堆码完成状态:重量 有->无
     * 6 起吊完成状态:重量 无->有
     *
    println(Global.RunningStatus.DM_FINISHED.name)//DM_FINISHED
    println(Global.RunningStatus.DM_FINISHED.ordinal)//4
    println(Global.RunningStatus.DM_FINISHED.data)//5
     */

    enum class RunningStatus(val data: Int) {
        STANDBY(1), NOLOAD(2), WITHLOAD(3), FAULT(4), DM_FINISHED(5), QD_FINISHED(6)
    }
}